package com.threeox.commonlibrary.adapter.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;

import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.util.CommonUtils;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 *
 * @ClassName: CommonExpandAdapter 
 *
 * @Description: TODO(BaseExpandableListAdapter 的基类) 
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016-5-18 上午10:56:18
 * 
 * @param <T>
 */
@SuppressWarnings("rawtypes")
public abstract class CommonExpandAdapter<T> extends BaseExpandableListAdapter implements IAdapter<T> {

	protected List<T> mDatas;
	protected Context mContext;
	protected String mChildName;
	protected int mGroupLayoutId;
	protected int mChildLayoutId;
	protected LayoutInflater mInflater;
	protected ExpandableListView mListView;
    protected boolean mScrollState = false;// 默认为不滚动状态

	private String TAG = this.getClass().getName();

	public CommonExpandAdapter() {
		super();
	}

	public CommonExpandAdapter(Context context, List<T> datas, String childName) {
		this.init(context, datas, childName);
	}

	public CommonExpandAdapter(Context context, List<T> datas, String childName, int groupLayoutId, int childLayoutId) {
		this.init(context, datas, childName, groupLayoutId, childLayoutId);
	}

	public CommonExpandAdapter init(Context context, List<T> datas, String childName) {
		return this.init(context, datas, childName, null, null);
	}

	public CommonExpandAdapter init(Context context, List<T> datas, String childName, Integer groupLayoutId, Integer childLayoutId) {
		this.mContext = context;
		this.mInflater = LayoutInflater.from(context);
		this.mDatas = datas;
		this.mChildName = childName;
		if (groupLayoutId != null)
			this.mGroupLayoutId = groupLayoutId;
		if (childLayoutId != null)
			this.mChildLayoutId = childLayoutId;
		return this;
	}

	@Override
	public int getGroupCount() {
		return mDatas.size();
	}

	/**
	 * 得到Group当前的对象
	 */
	@Override
	public T getGroup(int groupPosition) {
		return mDatas.get(groupPosition);
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		try {
			Object child = getChildrenVal(groupPosition);
			if (child instanceof List) {
				return ((List) child).size();
			} else {
				LogUtils.e(TAG, "getChildrenCount请选择Child为集合数据!");
				return 0;
			}
		} catch (Exception e) {
			LogUtils.e(TAG, "getChildrenCount出错了:" + e.getMessage());
			return 0;
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		try {
			Object child = getChildrenVal(groupPosition);
			if (child instanceof List) {
				return ((List) child).get(childPosition);
			} else {
				LogUtils.e(TAG, "请选择Child为集合数据!");
				return null;
			}
		} catch (Exception e) {
			LogUtils.e(TAG, "getChild出错了:" + e.getMessage());
			return null;
		}
	}

	/**
	 * 得到Children的值
	 * 
	 * @param groupPosition
	 * @return
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	private Object getChildrenVal(int groupPosition) throws Exception {
		Object child = null;
		T group = getGroup(groupPosition);
		if (group instanceof JSONObject) {
			child = ((JSONObject) group).getJSONArray(mChildName);
		} else {
			Class<? extends Object> clazz = group.getClass();
			Method method = clazz.getMethod("get" + CommonUtils.captureName(mChildName));
			child = method.invoke(group);
		}
		return child;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	/**
	 * 获取显示指定组的视图对象
	 * 
	 * @param groupPosition
	 *            组位置
	 * @param isExpanded
	 *            该组是展开状态还是伸缩状态
	 * @param convertView
	 *            重用已有的视图对象
	 * @param parent
	 *            返回的视图对象始终依附于的视图组
	 * @return
	 */
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		BaseViewHolder holder = null;
		try {
			int groupType = getGroupType(groupPosition);
			// 根据Item 设置布局
			holder = BaseViewHolder.get(mContext, convertView, parent, mGroupLayoutId, groupPosition);
			converGroupView(holder, groupPosition, groupType, isExpanded, getGroup(groupPosition));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return holder.getConvertView();
	}

	/**
	 * 转换为GroupView
	 * 
	 * @param holder
	 * @param groupPosition
	 * @param groupType
	 * @param t
	 */
	public abstract void converGroupView(BaseViewHolder holder, int groupPosition, int groupType, boolean isExpanded, T t);

	/**
	 * 
	 * 获取一个视图对象，显示指定组中的指定子元素数据。
	 * 
	 * @param groupPosition
	 *            组位置
	 * @param childPosition
	 *            子元素位置
	 * @param isLastChild
	 *            子元素是否处于组中的最后一个
	 * @param convertView
	 *            重用已有的视图(View)对象
	 * @param parent
	 *            返回的视图(View)对象始终依附于的视图组
	 * @return
	 */
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		BaseViewHolder holder = null;
		try {
			int childType = getChildType(groupPosition, childPosition);
			// 根据Item 设置布局
			holder = BaseViewHolder.get(mContext, convertView, parent,
					mChildLayoutId, childPosition);
			converChildView(holder, groupPosition, childPosition, childType,
					isLastChild, getChild(groupPosition, childPosition));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return holder.getConvertView();
	}

	/**
	 * 转换为GroupView
	 *
	 * @param holder
	 * @param groupPosition
	 * @param childPosition
	 * @param childType
	 * @param isLastChild
     * @param data
     */
	public abstract void converChildView(BaseViewHolder holder, int groupPosition, int childPosition, int childType, boolean isLastChild, Object data);

	// 孩子在指定的位置是可选的，即：arms中的元素是可点击的
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	// 表示孩子是否和组ID是跨基础数据的更改稳定
	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public CommonExpandAdapter remove(int position) {
		this.mDatas.remove(position);
		notifyDataSetChanged();
		return this;
	}

	@Override
	public CommonExpandAdapter remove(T t) {
		if (t != null) {
			this.mDatas.remove(t);
			notifyDataSetChanged();
		}
		return this;
	}

	@Override
	public CommonExpandAdapter removeAll() {
		if (mDatas != null) {
			this.mDatas.clear();
			notifyDataSetChanged();
		}
		return this;
	}

	@Override
	public CommonExpandAdapter update(int pos, T t) {
		if (t != null) {
			this.mDatas.set(pos, t);
			notifyDataSetChanged();
		}
		return this;
	}

	/**
	 * 添加多条数据到第pos 位
	 *
	 * @param pos
	 * @param list
	 */
	@Override
	public CommonExpandAdapter addAll(int pos, List<T> list) {
		if (EmptyUtils.isNotEmpty(list)) {
			this.mDatas.addAll(pos, list);
			notifyDataSetChanged();
		}
		return this;
	}

	@Override
	public CommonExpandAdapter addAll(List<T> list) {
		if (EmptyUtils.isNotEmpty(list)) {
			this.mDatas.addAll(list);
			notifyDataSetChanged();
		}
		return this;
	}


	@Override
	public CommonExpandAdapter add(int pos, T t) {
		if (EmptyUtils.isNotEmpty(t)) {
			this.mDatas.add(pos, t);
			notifyDataSetChanged();
		}
		return this;
	}

	@Override
	public CommonExpandAdapter add(T t) {
		if (t != null) {
			this.mDatas.add(t);
			notifyDataSetChanged();
		}
		return this;
	}
	
	public void setListView(ExpandableListView mListView) {
		this.mListView = mListView;
	}
	
	@Override
	public List<T> getDatas() {
		return this.mDatas;
	}
	
	@Override
	public int getCount() {
		return this.mDatas.size();
	}

	@Override
	public void setScrollState(boolean scrollState) {
        this.mScrollState = scrollState;
	}

	/*==============Activity和Fragment的生命周期==================*/

	@Override
	public void onCreate(Bundle savedInstanceState) {

	}

	@Override
	public boolean onStart() {
		return true;
	}

	@Override
	public boolean onRestart() {
		return true;
	}

	@Override
	public boolean onResume() {
		return true;
	}

	@Override
	public boolean onStop() {
		return true;
	}

	@Override
	public boolean onPause() {
		return true;
	}

	@Override
	public boolean onDestroy() {
		return true;
	}

	@Override
	public boolean onBackPressed() {
		return true;
	}

	@Override
	public void onAttach(Activity activity) {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return null;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

	}

	@Override
	public boolean onDestroyView() {
		return true;
	}

	@Override
	public boolean onDetach() {
		return true;
	}

    /*==============Activity和Fragment的生命周期==================*/

}
